package com.eshop.modules.shop.rest;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.modules.aop.ForbidSubmit;
import com.alibaba.fastjson.JSONObject;
import cn.hutool.core.util.ObjectUtil;
import com.eshop.utils.RedisUtil;
import com.eshop.modules.mp.config.WxMaConfiguration;
import com.eshop.modules.mp.config.WxPayConfiguration;
import com.eshop.modules.mp.config.WxMpConfiguration;
import com.eshop.modules.shop.domain.SystemConfig;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.shop.service.dto.SystemConfigQueryCriteria;
import com.eshop.modules.shop.service.SystemConfigService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "商城:配置管理" })
@RestController
@RequestMapping({ "api" })
public class SystemConfigController
{
    private final SystemConfigService systemConfigService;
    
    public SystemConfigController(final SystemConfigService systemConfigService) {
        this.systemConfigService = systemConfigService;
    }
    
    @Log("查询")
    @ApiOperation("查询")
    @GetMapping({ "/yxSystemConfig" })
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMCONFIG_ALL','YXSYSTEMCONFIG_SELECT')")
    public ResponseEntity getYxSystemConfigs(final SystemConfigQueryCriteria criteria, final Pageable pageable) {
        return new ResponseEntity(this.systemConfigService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @ForbidSubmit
    @Log("新增或修改")
    @ApiOperation("新增或修改")
    @PostMapping({ "/yxSystemConfig" })
    @CacheEvict(cacheNames = { "yshop:index_data" }, allEntries = true)
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMCONFIG_ALL','YXSYSTEMCONFIG_CREATE')")
    public ResponseEntity create(@RequestBody final String jsonStr) {
        final JSONObject jsonObject = JSON.parseObject(jsonStr);
        jsonObject.forEach((key, value) -> {
            SystemConfig systemConfig = (SystemConfig)this.systemConfigService.getOne(Wrappers.<SystemConfig>lambdaQuery().eq(SystemConfig::getMenuName, key));
            SystemConfig systemConfigModel = new SystemConfig();
            systemConfigModel.setMenuName(key);
            systemConfigModel.setValue(value.toString());
            if ("wechat_appid".equals(key)) {
                WxMpConfiguration.removeWxMpService();
                WxPayConfiguration.removeWxPayService();
                WxMaConfiguration.removeWxMaService();
            }
            if ("wxpay_mchId".equals(key) || "wxapp_appId".equals(key)) {
                WxPayConfiguration.removeWxPayService();
            }
            if ("exp_appId".equals(key)) {
                RedisUtil.del(new String[] { "yshop_express_service" });
            }
            RedisUtil.set(key, value.toString(), 0L);
            if (ObjectUtil.isNull(systemConfig)) {
                this.systemConfigService.save(systemConfigModel);
            }
            else {
                systemConfigModel.setId(systemConfig.getId());
                this.systemConfigService.saveOrUpdate(systemConfigModel);
            }
            return;
        });
        return new ResponseEntity(HttpStatus.CREATED);
    }
}
